import com.baizhiedu.basic.mapper.UserMapper;
import com.baizhiedu.basic.pojo.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: robert.song
 * @Date: Created in 2021/12/13
 */

@Slf4j
public class MybatisTestByXML {

    @org.junit.Test
    public void mybatisQuickStart1() throws IOException {
//        加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//        获取sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        获取事务对象，无参表示需要手动commit
        SqlSession sqlSession = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
//        验证sqlSessionFactory到底是不是单例模式
        System.out.println("sqlSession：" + sqlSession + ", sqlSession2:" + sqlSession2);
//        查询操作
//        selectAll(sqlSession);
//        新增操作
        save(sqlSession);
//        更新操作
//        update(sqlSession);
//        删除操作
//        delete(sqlSession);
//        基于mapper层实现查询
//        saveByMapperInf(sqlSession);
//        selectByCondition(sqlSession);
//        selectByMultiIds(sqlSession);
//        saveForeach(sqlSession);
    }

    public void selectAll(SqlSession sqlSession) {
//        userMapper是指为 mapper.xml的<mapper>标签定义的id
        List list = sqlSession.selectList("userMapper.findAll");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            log.debug(iterator.next().toString());
        }
    }

    public void save(SqlSession sqlSession) {
        User user = new User(null, "宋亚辉", "root");
        int result = sqlSession.update("userMapper.save", user);
        sqlSession.commit();
        log.debug(String.valueOf(result));
        log.debug(String.valueOf(user.getId()));
    }

    public void saveForeach(SqlSession sqlSession) {
        List<User> list = new ArrayList<>();
        User user1 = new User(null, "宋亚辉", "root");
        User user2 = new User(null, "宋亚辉", "root");
        User user3 = new User(null, "宋亚辉", "root");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(userMapper.hashCode());
        int result = userMapper.saveForeach(list);
        System.out.println(result);
        sqlSession.commit();
    }

    public void update(SqlSession sqlSession) {
        User user = new User(null, "宋亚辉", "root1");
        int result = sqlSession.update("userMapper.update", user);
        sqlSession.commit();
        log.debug(String.valueOf(result));
    }

    public void delete(SqlSession sqlSession) {
        int result = sqlSession.delete("userMapper.delete", "1341515");
        sqlSession.commit();
        log.debug(String.valueOf(result));
    }

    public void saveByMapperInf(SqlSession sqlSession) {
        UserMapper result = sqlSession.getMapper(UserMapper.class);
        List<User> list = result.findAll();
        log.debug(list.toString());
    }

    public void selectByCondition(SqlSession sqlSession) {
        Page<User> page = PageHelper.startPage(1, 10);
        User user = new User(null, "宋亚辉", null);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.findByCondition(user);
        System.out.println("page.total：" + page.getTotal());
        Iterator<User> iterator = users.iterator();
        while (iterator.hasNext()) {
            log.debug("users：" + iterator.next().toString());
        }
    }

    public void selectByMultiIds(SqlSession sqlSession) {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        List<User> users = userMapper.findInMultiIds(list);
        log.debug(users.toString());
    }
}
